<?php

require_once('config/maptrick.conf.php');
require_once(SITE_ROOT . 'config/constants.conf.php');
require_once(SITE_ROOT . 'lib/SmartyMaptrick.class.php');
require_once(SITE_ROOT . 'class/logic/Registration.class.php');
require_once(SITE_ROOT . 'class/logic/LogicBotmap.class.php');
require_once(SITE_ROOT . 'class/bean/Team.class.php');
require_once(SITE_ROOT . 'class/bean/League.class.php');
require_once(SITE_ROOT . 'config/session.php');

$logic = LogicBotmap::getInstance();
$smarty = new SmartyMaptrick();

if (count($_POST) > 0 && ($_POST["rnd"] != $_SESSION["rnd_maptrick"] || $_POST["rnd"] == "" || !isset($_POST["rnd"]))) {
	$_SESSION = array();
	$_POST = array();
}

if (count($_GET) > 0) {
	if (isset($_GET['country'])) $countryID = $_GET['country'];
	if (isset($_GET['type'])) $type = $_GET['type'];
}
else {
	if (isset($_SESSION['country_botmap'])) $countryID = $_SESSION['country_botmap'];
	if (isset($_SESSION['type_botmap'])) $type = $_SESSION['type_botmap'];
}

if (!preg_match("/^[0-9]{1,3}$/", $countryID) || !isset($countryID)) $countryID = 1;
$_SESSION['country_botmap'] = $countryID;

if (!isset($type)) {
	$type = CONST_BOTS;
}
$_SESSION['type_botmap'] = $type;

if (preg_match("/^[A-Za-z_-]{1,32}$/", $_POST['hidden'])) $hidden = $_POST['hidden'];

if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier']) && count($_POST) == 0) {
	try {
		$register = new Registration();
		$register->connect($_GET['oauth_token'], $_GET['oauth_verifier']);
	}
	catch (Exception $e) {
		$smarty->assign('message', $e->getMessage());
	}
}

if ($hidden == 'login') {
	try {
		$register = new Registration();
		$callback = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
		$url = $register->login($callback);
	}
	catch (Exception $e) {
		$smarty->assign('message', $e->getMessage());
	}
}
else if ($hidden == 'logout') {
	try {
		$register = new Registration();
		$register->logout();
	}
	catch (Exception $e) {
		$smarty->assign('message', $e->getMessage());
	}
}

if ($hidden == 'login') {
	header('Location: '. $url);
}
else {
	$smarty->assign('action', $_SERVER['PHP_SELF']);
	$smarty->assign('loggedIn', $_SESSION['login']);

	// assign the important attributes for the header
	$smarty->assign('title', 'Maptrick');
	$smarty->assign('bot_active', 'active');

	$_SESSION["rnd_maptrick"] = hash_hmac("sha256", rand(1000000000, 9999999999), SCRIPT_PWD);
	$smarty->assign("rnd_maptrick", $_SESSION["rnd_maptrick"]);
	
	if ($_SESSION['login'] && isset($_SESSION['login_team'])) {
		$login_team = $_SESSION['login_team'];
		$smarty->assign('login_user', $login_team->getUserName());
		$smarty->assign('login_team', $login_team->getName());
		$smarty->assign('login_teamID', $login_team->getTeamID());
		if ($login_team->getLeague() != null) {
			$smarty->assign('login_leagueID', $login_team->getLeague()->getLeagueID());
			$smarty->assign('login_league', $login_team->getLeague()->getName());
		}
	}

	$smarty->assign('introduction', "<p>The Botmap is a collection of statistics and data regarding the bot teams within Hattrick. There is a page for each country with a \"map\" for each division showing the distribution of bots among the individual leagues as squares of different colours. There is also a bar graph for each division, and one for the entire country. If you choose \"Supporters\" statistics will be displayed which show you how many managers in each league have paid for supporter.</p><p>Another statistic is presented here: \"percentage bot games\" describes how big a part of their league games managers in the given country or division will on average play against bots instead of teams managed by humans.</p>");
	
	$smarty->assign('countries', $logic->getCountries());
	$smarty->assign('selected_country', $countryID);

	if ($type == CONST_SUPPORTERS) {
		if ($file = fopen(SITE_ROOT . "botmap/s" . $countryID . ".data", "r")) {
			$js=fgets($file);
			fclose($file);
		}
	}
	else {
		if ($file = fopen(SITE_ROOT . "botmap/" . $countryID . ".data", "r")) {
			$js=fgets($file);
			fclose($file);
		}
	}
	$smarty->assign('botmap', $type == CONST_BOTS);
	$smarty->assign('supportermap', $type == CONST_SUPPORTERS);

	$smarty->assign('js', $js);

	$smarty->display('header.tpl');
	$smarty->display('botmap.tpl');
	$smarty->display('footer.tpl');
}

?>